{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.11 \u901a\u8fc7\u94a9\u5b50\u8fdc\u7a0b\u52a0\u8f7d\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u81ea\u5b9a\u4e49Python\u7684import\u8bed\u53e5\uff0c\u4f7f\u5f97\u5b83\u80fd\u4ece\u8fdc\u7a0b\u673a\u5668\u4e0a\u9762\u900f\u660e\u7684\u52a0\u8f7d\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\u8981\u63d0\u51fa\u6765\u7684\u662f\u5b89\u5168\u95ee\u9898\u3002\u672c\u8282\u8ba8\u8bba\u7684\u601d\u60f3\u5982\u679c\u6ca1\u6709\u4e00\u4e9b\u989d\u5916\u7684\u5b89\u5168\u548c\u8ba4\u77e5\u673a\u5236\u7684\u8bdd\u4f1a\u5f88\u7cdf\u7cd5\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u7684\u4e3b\u8981\u76ee\u7684\u662f\u6df1\u5165\u5206\u6790Python\u7684import\u8bed\u53e5\u673a\u5236\u3002\n\u5982\u679c\u4f60\u7406\u89e3\u4e86\u672c\u8282\u5185\u90e8\u539f\u7406\uff0c\u4f60\u5c31\u80fd\u591f\u4e3a\u5176\u4ed6\u4efb\u4f55\u76ee\u7684\u800c\u81ea\u5b9a\u4e49import\u3002\n\u6709\u4e86\u8fd9\u4e9b\uff0c\u8ba9\u6211\u4eec\u7ee7\u7eed\u5411\u524d\u8d70\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6838\u5fc3\u662f\u8bbe\u8ba1\u5bfc\u5165\u8bed\u53e5\u7684\u6269\u5c55\u529f\u80fd\u3002\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u8fd9\u4e2a\uff0c\n\u4e0d\u8fc7\u4e3a\u4e86\u6f14\u793a\u7684\u65b9\u4fbf\uff0c\u6211\u4eec\u5f00\u59cb\u5148\u6784\u9020\u4e0b\u9762\u8fd9\u4e2aPython\u4ee3\u7801\u7ed3\u6784\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "testcode/\n    spam.py\n    fib.py\n    grok/\n        __init__.py\n        blah.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u4e0d\u91cd\u8981\uff0c\u4e0d\u8fc7\u6211\u4eec\u5728\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u653e\u5165\u4e86\u5c11\u91cf\u7684\u7b80\u5355\u8bed\u53e5\u548c\u51fd\u6570\uff0c\n\u8fd9\u6837\u4f60\u53ef\u4ee5\u6d4b\u8bd5\u5b83\u4eec\u5e76\u67e5\u770b\u5f53\u5b83\u4eec\u88ab\u5bfc\u5165\u65f6\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# spam.py\nprint(\"I'm spam\")\n\ndef hello(name):\n    print('Hello %s' % name)\n\n# fib.py\nprint(\"I'm fib\")\n\ndef fib(n):\n    if n < 2:\n        return 1\n    else:\n        return fib(n-1) + fib(n-2)\n\n# grok/__init__.py\nprint(\"I'm grok.__init__\")\n\n# grok/blah.py\nprint(\"I'm grok.blah\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684\u76ee\u7684\u662f\u5141\u8bb8\u8fd9\u4e9b\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u88ab\u8fdc\u7a0b\u8bbf\u95ee\u3002\n\u4e5f\u8bb8\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u5c06\u5b83\u4eec\u53d1\u5e03\u5230\u4e00\u4e2aweb\u670d\u52a1\u5668\u4e0a\u9762\u3002\u5728testcode\u76ee\u5f55\u4e2d\u50cf\u4e0b\u9762\u8fd9\u6837\u8fd0\u884cPython\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % cd testcode\nbash % python3 -m http.server 15000\nServing HTTP on 0.0.0.0 port 15000 ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u670d\u52a1\u5668\u8fd0\u884c\u8d77\u6765\u540e\u518d\u542f\u52a8\u4e00\u4e2a\u5355\u72ec\u7684Python\u89e3\u91ca\u5668\u3002\n\u786e\u4fdd\u4f60\u53ef\u4ee5\u4f7f\u7528 urllib \u8bbf\u95ee\u5230\u8fdc\u7a0b\u6587\u4ef6\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib.request import urlopen\nu = urlopen('http://localhost:15000/fib.py')\ndata = u.read().decode('utf-8')\nprint(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ece\u8fd9\u4e2a\u670d\u52a1\u5668\u52a0\u8f7d\u6e90\u4ee3\u7801\u662f\u63a5\u4e0b\u6765\u672c\u8282\u7684\u57fa\u7840\u3002\n\u4e3a\u4e86\u66ff\u4ee3\u624b\u52a8\u7684\u901a\u8fc7 urlopen() \u6765\u6536\u96c6\u6e90\u6587\u4ef6\uff0c\n\u6211\u4eec\u901a\u8fc7\u81ea\u5b9a\u4e49import\u8bed\u53e5\u6765\u5728\u540e\u53f0\u81ea\u52a8\u5e2e\u6211\u4eec\u505a\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u52a0\u8f7d\u8fdc\u7a0b\u6a21\u5757\u7684\u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u663e\u5f0f\u7684\u52a0\u8f7d\u51fd\u6570\u6765\u5b8c\u6210\u5b83\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import imp\nimport urllib.request\nimport sys\n\ndef load_module(url):\n    u = urllib.request.urlopen(url)\n    source = u.read().decode('utf-8')\n    mod = sys.modules.setdefault(url, imp.new_module(url))\n    code = compile(source, url, 'exec')\n    mod.__file__ = url\n    mod.__package__ = ''\n    exec(code, mod.__dict__)\n    return mod"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e0b\u8f7d\u6e90\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528 compile() \u5c06\u5176\u7f16\u8bd1\u5230\u4e00\u4e2a\u4ee3\u7801\u5bf9\u8c61\u4e2d\uff0c\n\u7136\u540e\u5728\u4e00\u4e2a\u65b0\u521b\u5efa\u7684\u6a21\u5757\u5bf9\u8c61\u7684\u5b57\u5178\u4e2d\u6765\u6267\u884c\u5b83\u3002\u4e0b\u9762\u662f\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65b9\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib = load_module('http://localhost:15000/fib.py')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib.fib(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam = load_module('http://localhost:15000/spam.py')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam.hello('Guido')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u5bf9\u4e8e\u7b80\u5355\u7684\u6a21\u5757\u8fd9\u4e2a\u662f\u884c\u5f97\u901a\u7684\u3002\n\u4e0d\u8fc7\u5b83\u5e76\u6ca1\u6709\u5d4c\u5165\u5230\u901a\u5e38\u7684import\u8bed\u53e5\u4e2d\uff0c\u5982\u679c\u8981\u652f\u6301\u66f4\u9ad8\u7ea7\u7684\u7ed3\u6784\u6bd4\u5982\u5305\u5c31\u9700\u8981\u66f4\u591a\u7684\u5de5\u4f5c\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u66f4\u9177\u7684\u505a\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bfc\u5165\u5668\u3002\u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u5143\u8def\u5f84\u5bfc\u5165\u5668\u3002\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# urlimport.py\nimport sys\nimport importlib.abc\nimport imp\nfrom urllib.request import urlopen\nfrom urllib.error import HTTPError, URLError\nfrom html.parser import HTMLParser\n\n# Debugging\nimport logging\nlog = logging.getLogger(__name__)\n\n# Get links from a given URL\ndef _get_links(url):\n    class LinkParser(HTMLParser):\n        def handle_starttag(self, tag, attrs):\n            if tag == 'a':\n                attrs = dict(attrs)\n                links.add(attrs.get('href').rstrip('/'))\n    links = set()\n    try:\n        log.debug('Getting links from %s' % url)\n        u = urlopen(url)\n        parser = LinkParser()\n        parser.feed(u.read().decode('utf-8'))\n    except Exception as e:\n        log.debug('Could not get links. %s', e)\n    log.debug('links: %r', links)\n    return links\n\nclass UrlMetaFinder(importlib.abc.MetaPathFinder):\n    def __init__(self, baseurl):\n        self._baseurl = baseurl\n        self._links = { }\n        self._loaders = { baseurl : UrlModuleLoader(baseurl) }\n\n    def find_module(self, fullname, path=None):\n        log.debug('find_module: fullname=%r, path=%r', fullname, path)\n        if path is None:\n            baseurl = self._baseurl\n        else:\n            if not path[0].startswith(self._baseurl):\n                return None\n            baseurl = path[0]\n        parts = fullname.split('.')\n        basename = parts[-1]\n        log.debug('find_module: baseurl=%r, basename=%r', baseurl, basename)\n\n        # Check link cache\n        if basename not in self._links:\n            self._links[baseurl] = _get_links(baseurl)\n\n        # Check if it's a package\n        if basename in self._links[baseurl]:\n            log.debug('find_module: trying package %r', fullname)\n            fullurl = self.base_url + '/' + basename\n            # Attempt to load the package (which accesses __init__.py)\n            loader = UrlPackageLoader(fullurl)\n            try:\n                loader.load_module(fullname)\n                self._links[fullurl] = _get_links(fullurl)\n                self._loaders[fullurl] = UrlModuleLoader(fullurl)\n                log.debug('find_module: package %r loaded', fullname)\n            except ImportError as e:\n                log.debug('find_module: package failed. %s', e)\n                loader = None\n            return loader\n        # A normal module\n        filename = basename + '.py'\n        if filename in self._links[baseurl]:\n            log.debug('find_module: module %r found', fullname)\n            return self._loaders[baseurl]\n        else:\n            log.debug('find_module: module %r not found', fullname)\n            return None\n\n    def invalidate_caches(self):\n        log.debug('invalidating link cache')\n        self._links.clear()\n\n# Module Loader for a URL\nclass UrlModuleLoader(importlib.abc.SourceLoader):\n    def __init__(self, baseurl):\n        self._baseurl = baseurl\n        self._source_cache = {}\n\n    def module_repr(self, module):\n        return '<urlmodule %r from %r>' % (module.__name__, module.__file__)\n\n    # Required method\n    def load_module(self, fullname):\n        code = self.get_code(fullname)\n        mod = sys.modules.setdefault(fullname, imp.new_module(fullname))\n        mod.__file__ = self.get_filename(fullname)\n        mod.__loader__ = self\n        mod.__package__ = fullname.rpartition('.')[0]\n        exec(code, mod.__dict__)\n        return mod\n\n    # Optional extensions\n    def get_code(self, fullname):\n        src = self.get_source(fullname)\n        return compile(src, self.get_filename(fullname), 'exec')\n\n    def get_data(self, path):\n        pass\n\n    def get_filename(self, fullname):\n        return self._baseurl + '/' + fullname.split('.')[-1] + '.py'\n\n    def get_source(self, fullname):\n        filename = self.get_filename(fullname)\n        log.debug('loader: reading %r', filename)\n        if filename in self._source_cache:\n            log.debug('loader: cached %r', filename)\n            return self._source_cache[filename]\n        try:\n            u = urlopen(filename)\n            source = u.read().decode('utf-8')\n            log.debug('loader: %r loaded', filename)\n            self._source_cache[filename] = source\n            return source\n        except (HTTPError, URLError) as e:\n            log.debug('loader: %r failed. %s', filename, e)\n            raise ImportError(\"Can't load %s\" % filename)\n\n    def is_package(self, fullname):\n        return False\n\n# Package loader for a URL\nclass UrlPackageLoader(UrlModuleLoader):\n    def load_module(self, fullname):\n        mod = super().load_module(fullname)\n        mod.__path__ = [ self._baseurl ]\n        mod.__package__ = fullname\n\n    def get_filename(self, fullname):\n        return self._baseurl + '/' + '__init__.py'\n\n    def is_package(self, fullname):\n        return True\n\n# Utility functions for installing/uninstalling the loader\n_installed_meta_cache = { }\ndef install_meta(address):\n    if address not in _installed_meta_cache:\n        finder = UrlMetaFinder(address)\n        _installed_meta_cache[address] = finder\n        sys.meta_path.append(finder)\n        log.debug('%r installed on sys.meta_path', finder)\n\ndef remove_meta(address):\n    if address in _installed_meta_cache:\n        finder = _installed_meta_cache.pop(address)\n        sys.meta_path.remove(finder)\n        log.debug('%r removed from sys.meta_path', finder)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4ea4\u4e92\u4f1a\u8bdd\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u524d\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# importing currently fails\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Load the importer and retry (it works)\nimport urlimport\nurlimport.install_meta('http://localhost:15000')\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import spam"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import grok.blah"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grok.blah.__file__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u7279\u6b8a\u7684\u65b9\u6848\u4f1a\u5b89\u88c5\u4e00\u4e2a\u7279\u522b\u7684\u67e5\u627e\u5668 UrlMetaFinder \u5b9e\u4f8b\uff0c\n\u4f5c\u4e3a sys.meta_path \u4e2d\u6700\u540e\u7684\u5b9e\u4f53\u3002\n\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c\u4f1a\u4f9d\u636e sys.meta_path \u4e2d\u7684\u67e5\u627e\u5668\u5b9a\u4f4d\u6a21\u5757\u3002\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cUrlMetaFinder \u5b9e\u4f8b\u662f\u6700\u540e\u4e00\u4e2a\u67e5\u627e\u5668\u65b9\u6848\uff0c\n\u5f53\u6a21\u5757\u5728\u4efb\u4f55\u4e00\u4e2a\u666e\u901a\u5730\u65b9\u90fd\u627e\u4e0d\u5230\u7684\u65f6\u5019\u5c31\u89e6\u53d1\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6848\uff0cUrlMetaFinder \u7c7b\u5305\u88c5\u5728\u4e00\u4e2a\u7528\u6237\u6307\u5b9a\u7684URL\u4e0a\u3002\n\u5728\u5185\u90e8\uff0c\u67e5\u627e\u5668\u901a\u8fc7\u6293\u53d6\u6307\u5b9aURL\u7684\u5185\u5bb9\u6784\u5efa\u5408\u6cd5\u7684\u94fe\u63a5\u96c6\u5408\u3002\n\u5bfc\u5165\u7684\u65f6\u5019\uff0c\u6a21\u5757\u540d\u4f1a\u8ddf\u5df2\u6709\u7684\u94fe\u63a5\u4f5c\u5bf9\u6bd4\u3002\u5982\u679c\u627e\u5230\u4e86\u4e00\u4e2a\u5339\u914d\u7684\uff0c\n\u4e00\u4e2a\u5355\u72ec\u7684 UrlModuleLoader \u7c7b\u88ab\u7528\u6765\u4ece\u8fdc\u7a0b\u673a\u5668\u4e0a\u52a0\u8f7d\u6e90\u4ee3\u7801\u5e76\u521b\u5efa\u6700\u7ec8\u7684\u6a21\u5757\u5bf9\u8c61\u3002\n\u8fd9\u91cc\u7f13\u5b58\u94fe\u63a5\u7684\u4e00\u4e2a\u539f\u56e0\u662f\u907f\u514d\u4e0d\u5fc5\u8981\u7684HTTP\u8bf7\u6c42\u91cd\u590d\u5bfc\u5165\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81ea\u5b9a\u4e49\u5bfc\u5165\u7684\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u7f16\u5199\u4e00\u4e2a\u94a9\u5b50\u76f4\u63a5\u5d4c\u5165\u5230 sys.path \u53d8\u91cf\u4e2d\u53bb\uff0c\n\u8bc6\u522b\u67d0\u4e9b\u76ee\u5f55\u547d\u540d\u6a21\u5f0f\u3002\n\u5728 urlimport.py \u4e2d\u6dfb\u52a0\u5982\u4e0b\u7684\u7c7b\u548c\u652f\u6301\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# urlimport.py\n# ... include previous code above ...\n# Path finder class for a URL\nclass UrlPathFinder(importlib.abc.PathEntryFinder):\n    def __init__(self, baseurl):\n        self._links = None\n        self._loader = UrlModuleLoader(baseurl)\n        self._baseurl = baseurl\n\n    def find_loader(self, fullname):\n        log.debug('find_loader: %r', fullname)\n        parts = fullname.split('.')\n        basename = parts[-1]\n        # Check link cache\n        if self._links is None:\n            self._links = [] # See discussion\n            self._links = _get_links(self._baseurl)\n\n        # Check if it's a package\n        if basename in self._links:\n            log.debug('find_loader: trying package %r', fullname)\n            fullurl = self._baseurl + '/' + basename\n            # Attempt to load the package (which accesses __init__.py)\n            loader = UrlPackageLoader(fullurl)\n            try:\n                loader.load_module(fullname)\n                log.debug('find_loader: package %r loaded', fullname)\n            except ImportError as e:\n                log.debug('find_loader: %r is a namespace package', fullname)\n                loader = None\n            return (loader, [fullurl])\n\n        # A normal module\n        filename = basename + '.py'\n        if filename in self._links:\n            log.debug('find_loader: module %r found', fullname)\n            return (self._loader, [])\n        else:\n            log.debug('find_loader: module %r not found', fullname)\n            return (None, [])\n\n    def invalidate_caches(self):\n        log.debug('invalidating link cache')\n        self._links = None\n\n# Check path to see if it looks like a URL\n_url_path_cache = {}\ndef handle_url(path):\n    if path.startswith(('http://', 'https://')):\n        log.debug('Handle path? %s. [Yes]', path)\n        if path in _url_path_cache:\n            finder = _url_path_cache[path]\n        else:\n            finder = UrlPathFinder(path)\n            _url_path_cache[path] = finder\n        return finder\n    else:\n        log.debug('Handle path? %s. [No]', path)\n\ndef install_path_hook():\n    sys.path_hooks.append(handle_url)\n    sys.path_importer_cache.clear()\n    log.debug('Installing handle_url')\n\ndef remove_path_hook():\n    sys.path_hooks.remove(handle_url)\n    sys.path_importer_cache.clear()\n    log.debug('Removing handle_url')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u4f7f\u7528\u8fd9\u4e2a\u8def\u5f84\u67e5\u627e\u5668\uff0c\u4f60\u53ea\u9700\u8981\u5728 sys.path \u4e2d\u52a0\u5165URL\u94fe\u63a5\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Initial import fails\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Install the path hook\nimport urlimport\nurlimport.install_path_hook()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Imports still fail (not on path)\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Add an entry to sys.path and watch it work\nimport sys\nsys.path.append('http://localhost:15000')\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import grok.blah"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grok.blah.__file__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u952e\u70b9\u5c31\u662f handle_url() \u51fd\u6570\uff0c\u5b83\u88ab\u6dfb\u52a0\u5230\u4e86 sys.path_hooks \u53d8\u91cf\u4e2d\u3002\n\u5f53 sys.path \u7684\u5b9e\u4f53\u88ab\u5904\u7406\u65f6\uff0c\u4f1a\u8c03\u7528 sys.path_hooks \u4e2d\u7684\u51fd\u6570\u3002\n\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u67e5\u627e\u5668\u5bf9\u8c61\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u88ab\u7528\u6765\u4e3a sys.path \u5b9e\u4f53\u52a0\u8f7d\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fdc\u7a0b\u6a21\u5757\u52a0\u8f7d\u8ddf\u5176\u4ed6\u7684\u52a0\u8f7d\u4f7f\u7528\u65b9\u6cd5\u51e0\u4e4e\u662f\u4e00\u6837\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib.__name__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib.__file__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import inspect\nprint(inspect.getsource(fib))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8be6\u7ec6\u8ba8\u8bba\u4e4b\u524d\uff0c\u6709\u70b9\u8981\u5f3a\u8c03\u7684\u662f\uff0cPython\u7684\u6a21\u5757\u3001\u5305\u548c\u5bfc\u5165\u673a\u5236\u662f\u6574\u4e2a\u8bed\u8a00\u4e2d\u6700\u590d\u6742\u7684\u90e8\u5206\uff0c\n\u5373\u4f7f\u7ecf\u9a8c\u4e30\u5bcc\u7684Python\u7a0b\u5e8f\u5458\u4e5f\u5f88\u5c11\u80fd\u7cbe\u901a\u5b83\u4eec\u3002\n\u6211\u5728\u8fd9\u91cc\u63a8\u8350\u4e00\u4e9b\u503c\u7684\u53bb\u8bfb\u7684\u6587\u6863\u548c\u4e66\u7c4d\uff0c\u5305\u62ec\nimportlib module\n\u548c PEP 302.\n\u6587\u6863\u5185\u5bb9\u5728\u8fd9\u91cc\u4e0d\u4f1a\u88ab\u91cd\u590d\u63d0\u5230\uff0c\u4e0d\u8fc7\u6211\u5728\u8fd9\u91cc\u4f1a\u8ba8\u8bba\u4e00\u4e9b\u6700\u91cd\u8981\u7684\u90e8\u5206\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u5982\u679c\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6a21\u5757\u5bf9\u8c61\uff0c\u4f7f\u7528 imp.new_module() \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import imp\nm = imp.new_module('spam')\nm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.__name__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6a21\u5757\u5bf9\u8c61\u901a\u5e38\u6709\u4e00\u4e9b\u671f\u671b\u5c5e\u6027\uff0c\u5305\u62ec __file__ \uff08\u8fd0\u884c\u6a21\u5757\u52a0\u8f7d\u8bed\u53e5\u7684\u6587\u4ef6\u540d\uff09\n\u548c __package__ (\u5305\u540d)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5176\u6b21\uff0c\u6a21\u5757\u4f1a\u88ab\u89e3\u91ca\u5668\u7f13\u5b58\u8d77\u6765\u3002\u6a21\u5757\u7f13\u5b58\u53ef\u4ee5\u5728\u5b57\u5178 sys.modules \u4e2d\u88ab\u627e\u5230\u3002\n\u56e0\u4e3a\u6709\u4e86\u8fd9\u4e2a\u7f13\u5b58\u673a\u5236\uff0c\u901a\u5e38\u53ef\u4ee5\u5c06\u7f13\u5b58\u548c\u6a21\u5757\u7684\u521b\u5efa\u901a\u8fc7\u4e00\u4e2a\u6b65\u9aa4\u5b8c\u6210\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nimport imp\nm = sys.modules.setdefault('spam', imp.new_module('spam'))\nm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u7ed9\u5b9a\u6a21\u5757\u5df2\u7ecf\u5b58\u5728\u90a3\u4e48\u5c31\u4f1a\u76f4\u63a5\u83b7\u5f97\u5df2\u7ecf\u88ab\u521b\u5efa\u8fc7\u7684\u6a21\u5757\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\nm = sys.modules.setdefault('math', imp.new_module('math'))\nm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.sin(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.cos(2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u521b\u5efa\u6a21\u5757\u5f88\u7b80\u5355\uff0c\u5f88\u5bb9\u6613\u7f16\u5199\u7b80\u5355\u51fd\u6570\u6bd4\u5982\u7b2c\u4e00\u90e8\u5206\u7684 load_module() \u51fd\u6570\u3002\n\u8fd9\u4e2a\u65b9\u6848\u7684\u4e00\u4e2a\u7f3a\u70b9\u662f\u5f88\u96be\u5904\u7406\u590d\u6742\u60c5\u51b5\u6bd4\u5982\u5305\u7684\u5bfc\u5165\u3002\n\u4e3a\u4e86\u5904\u7406\u4e00\u4e2a\u5305\uff0c\u4f60\u8981\u91cd\u65b0\u5b9e\u73b0\u666e\u901aimport\u8bed\u53e5\u7684\u5e95\u5c42\u903b\u8f91\uff08\u6bd4\u5982\u68c0\u67e5\u76ee\u5f55\uff0c\u67e5\u627e__init__.py\u6587\u4ef6\uff0c\n\u6267\u884c\u90a3\u4e9b\u6587\u4ef6\uff0c\u8bbe\u7f6e\u8def\u5f84\u7b49\uff09\u3002\u8fd9\u4e2a\u590d\u6742\u6027\u5c31\u662f\u4e3a\u4ec0\u4e48\u6700\u597d\u76f4\u63a5\u6269\u5c55import\u8bed\u53e5\u800c\u4e0d\u662f\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4e00\u4e2a\u539f\u56e0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6269\u5c55import\u8bed\u53e5\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u4f1a\u6709\u5f88\u591a\u79fb\u52a8\u64cd\u4f5c\u3002\n\u6700\u9ad8\u5c42\u4e0a\uff0c\u5bfc\u5165\u64cd\u4f5c\u88ab\u4e00\u4e2a\u4f4d\u4e8esys.meta_path\u5217\u8868\u4e2d\u7684\u201c\u5143\u8def\u5f84\u201d\u67e5\u627e\u5668\u5904\u7406\u3002\n\u5982\u679c\u4f60\u8f93\u51fa\u5b83\u7684\u503c\uff0c\u4f1a\u770b\u5230\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pprint import pprint\npprint(sys.meta_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6267\u884c\u4e00\u4e2a\u8bed\u53e5\u6bd4\u5982 import fib \u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u904d\u5386sys.mata_path\u4e2d\u7684\u67e5\u627e\u5668\u5bf9\u8c61\uff0c\n\u8c03\u7528\u5b83\u4eec\u7684 find_module() \u65b9\u6cd5\u5b9a\u4f4d\u6b63\u786e\u7684\u6a21\u5757\u52a0\u8f7d\u5668\u3002\n\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9a8c\u6765\u770b\u770b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Finder:\n    def find_module(self, fullname, path):\n        print('Looking for', fullname, path)\n        return None\nimport sys\nsys.meta_path.insert(0, Finder()) # Insert as first entry\nimport math"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import types"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u770b find_module() \u65b9\u6cd5\u662f\u600e\u6837\u5728\u6bcf\u4e00\u4e2a\u5bfc\u5165\u5c31\u88ab\u89e6\u53d1\u7684\u3002\n\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u7684path\u53c2\u6570\u7684\u4f5c\u7528\u662f\u5904\u7406\u5305\u3002\n\u591a\u4e2a\u5305\u88ab\u5bfc\u5165\uff0c\u5c31\u662f\u4e00\u4e2a\u53ef\u5728\u5305\u7684 __path__ \u5c5e\u6027\u4e2d\u627e\u5230\u7684\u8def\u5f84\u5217\u8868\u3002\n\u8981\u627e\u5230\u5305\u7684\u5b50\u7ec4\u4ef6\u5c31\u8981\u68c0\u67e5\u8fd9\u4e9b\u8def\u5f84\u3002\n\u6bd4\u5982\u6ce8\u610f\u5bf9\u4e8e xml.etree \u548c xml.etree.ElementTree \u7684\u8def\u5f84\u914d\u7f6e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import xml.etree.ElementTree"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 sys.meta_path \u4e0a\u67e5\u627e\u5668\u7684\u4f4d\u7f6e\u5f88\u91cd\u8981\uff0c\u5c06\u5b83\u4ece\u961f\u5934\u79fb\u5230\u961f\u5c3e\uff0c\u7136\u540e\u518d\u8bd5\u8bd5\u5bfc\u5165\u770b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "del sys.meta_path[0]\nsys.meta_path.append(Finder())\nimport urllib.request\nimport datetime"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u4f60\u770b\u4e0d\u5230\u4efb\u4f55\u8f93\u51fa\u4e86\uff0c\u56e0\u4e3a\u5bfc\u5165\u88absys.meta_path\u4e2d\u7684\u5176\u4ed6\u5b9e\u4f53\u5904\u7406\u3002\n\u8fd9\u65f6\u5019\uff0c\u4f60\u53ea\u6709\u5728\u5bfc\u5165\u4e0d\u5b58\u5728\u6a21\u5757\u7684\u65f6\u5019\u624d\u80fd\u770b\u5230\u5b83\u88ab\u89e6\u53d1\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import xml.superfast"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u4e4b\u524d\u5b89\u88c5\u8fc7\u4e00\u4e2a\u6355\u83b7\u672a\u77e5\u6a21\u5757\u7684\u67e5\u627e\u5668\uff0c\u8fd9\u4e2a\u662f UrlMetaFinder \u7c7b\u7684\u5173\u952e\u3002\n\u4e00\u4e2a UrlMetaFinder \u5b9e\u4f8b\u88ab\u6dfb\u52a0\u5230 sys.meta_path \u7684\u672b\u5c3e\uff0c\u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u67e5\u627e\u5668\u65b9\u6848\u3002\n\u5982\u679c\u88ab\u8bf7\u6c42\u7684\u6a21\u5757\u540d\u4e0d\u80fd\u5b9a\u4f4d\uff0c\u5c31\u4f1a\u88ab\u8fd9\u4e2a\u67e5\u627e\u5668\u5904\u7406\u6389\u3002\n\u5904\u7406\u5305\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\uff0c\u5728path\u53c2\u6570\u4e2d\u6307\u5b9a\u7684\u503c\u9700\u8981\u88ab\u68c0\u67e5\uff0c\u770b\u5b83\u662f\u5426\u4ee5\u67e5\u627e\u5668\u4e2d\u6ce8\u518c\u7684URL\u5f00\u5934\u3002\n\u5982\u679c\u4e0d\u662f\uff0c\u8be5\u5b50\u6a21\u5757\u5fc5\u987b\u5f52\u5c5e\u4e8e\u5176\u4ed6\u67e5\u627e\u5668\u5e76\u88ab\u5ffd\u7565\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5305\u7684\u5176\u4ed6\u5904\u7406\u53ef\u5728 UrlPackageLoader \u7c7b\u4e2d\u88ab\u627e\u5230\u3002\n\u8fd9\u4e2a\u7c7b\u4e0d\u4f1a\u5bfc\u5165\u5305\u540d\uff0c\u800c\u662f\u53bb\u52a0\u8f7d\u5bf9\u5e94\u7684 __init__.py \u6587\u4ef6\u3002\n\u5b83\u4e5f\u4f1a\u8bbe\u7f6e\u6a21\u5757\u7684 __path__ \u5c5e\u6027\uff0c\u8fd9\u4e00\u6b65\u5f88\u91cd\u8981\uff0c\n\u56e0\u4e3a\u5728\u52a0\u8f7d\u5305\u7684\u5b50\u6a21\u5757\u65f6\u8fd9\u4e2a\u503c\u4f1a\u88ab\u4f20\u7ed9\u540e\u9762\u7684 find_module() \u8c03\u7528\u3002\n\u57fa\u4e8e\u8def\u5f84\u7684\u5bfc\u5165\u94a9\u5b50\u662f\u8fd9\u4e9b\u601d\u60f3\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u4f46\u662f\u91c7\u7528\u4e86\u53e6\u5916\u7684\u65b9\u6cd5\u3002\n\u6211\u4eec\u90fd\u77e5\u9053\uff0csys.path \u662f\u4e00\u4e2aPython\u67e5\u627e\u6a21\u5757\u7684\u76ee\u5f55\u5217\u8868\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pprint import pprint\nimport sys\npprint(sys.path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 sys.path \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b9e\u4f53\u90fd\u4f1a\u88ab\u989d\u5916\u7684\u7ed1\u5b9a\u5230\u4e00\u4e2a\u67e5\u627e\u5668\u5bf9\u8c61\u4e0a\u3002\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b sys.path_importer_cache \u53bb\u770b\u4e0b\u8fd9\u4e9b\u67e5\u627e\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pprint(sys.path_importer_cache)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "sys.path_importer_cache \u6bd4 sys.path \u4f1a\u66f4\u5927\u70b9\uff0c\n\u56e0\u4e3a\u5b83\u4f1a\u4e3a\u6240\u6709\u88ab\u52a0\u8f7d\u4ee3\u7801\u7684\u76ee\u5f55\u8bb0\u5f55\u5b83\u4eec\u7684\u67e5\u627e\u5668\u3002\n\u8fd9\u5305\u62ec\u5305\u7684\u5b50\u76ee\u5f55\uff0c\u8fd9\u4e9b\u901a\u5e38\u5728 sys.path \u4e2d\u662f\u4e0d\u5b58\u5728\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6267\u884c import fib \uff0c\u4f1a\u987a\u5e8f\u68c0\u67e5 sys.path \u4e2d\u7684\u76ee\u5f55\u3002\n\u5bf9\u4e8e\u6bcf\u4e2a\u76ee\u5f55\uff0c\u540d\u79f0\u201cfib\u201d\u4f1a\u88ab\u4f20\u7ed9\u76f8\u5e94\u7684 sys.path_importer_cache \u4e2d\u7684\u67e5\u627e\u5668\u3002\n\u8fd9\u4e2a\u53ef\u4ee5\u8ba9\u4f60\u521b\u5efa\u81ea\u5df1\u7684\u67e5\u627e\u5668\u5e76\u5728\u7f13\u5b58\u4e2d\u653e\u5165\u4e00\u4e2a\u5b9e\u4f53\u3002\u8bd5\u8bd5\u8fd9\u4e2a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Finder:\ndef find_loader(self, name):\n    print('Looking for', name)\n    return (None, [])\nimport sys\n# Add a \"debug\" entry to the importer cache\nsys.path_importer_cache['debug'] = Finder()\n# Add a \"debug\" directory to sys.path\nsys.path.insert(0, 'debug')\nimport threading"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\uff0c\u4f60\u53ef\u4ee5\u4e3a\u540d\u5b57\u201cdebug\u201d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7f13\u5b58\u5b9e\u4f53\u5e76\u5c06\u5b83\u8bbe\u7f6e\u6210 sys.path \u4e0a\u7684\u7b2c\u4e00\u4e2a\u3002\n\u5728\u6240\u6709\u63a5\u4e0b\u6765\u7684\u5bfc\u5165\u4e2d\uff0c\u4f60\u4f1a\u770b\u5230\u4f60\u7684\u67e5\u627e\u5668\u88ab\u89e6\u53d1\u4e86\u3002\n\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5b83\u8fd4\u56de (None, [])\uff0c\u90a3\u4e48\u5904\u7406\u8fdb\u7a0b\u4f1a\u7ee7\u7eed\u5904\u7406\u4e0b\u4e00\u4e2a\u5b9e\u4f53\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "sys.path_importer_cache \u7684\u4f7f\u7528\u88ab\u4e00\u4e2a\u5b58\u50a8\u5728 sys.path_hooks \u4e2d\u7684\u51fd\u6570\u5217\u8868\u63a7\u5236\u3002\n\u8bd5\u8bd5\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u5b83\u4f1a\u6e05\u9664\u7f13\u5b58\u5e76\u7ed9 sys.path_hooks \u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u8def\u5f84\u68c0\u67e5\u51fd\u6570"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.path_importer_cache.clear()\ndef check_path(path):\n    print('Checking', path)\n    raise ImportError()\nsys.path_hooks.insert(0, check_path)\nimport fib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0ccheck_path() \u51fd\u6570\u88ab\u6bcf\u4e2a sys.path \u4e2d\u7684\u5b9e\u4f53\u8c03\u7528\u3002\n\u4e0d\u987e\uff0c\u7531\u4e8e\u629b\u51fa\u4e86 ImportError \u5f02\u5e38\uff0c\n\u5565\u90fd\u4e0d\u4f1a\u53d1\u751f\u4e86\uff08\u4ec5\u4ec5\u5c06\u68c0\u67e5\u8f6c\u79fb\u5230sys.path_hooks\u7684\u4e0b\u4e00\u4e2a\u51fd\u6570\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u77e5\u9053\u4e86\u600e\u6837sys.path\u662f\u600e\u6837\u88ab\u5904\u7406\u7684\uff0c\u4f60\u5c31\u80fd\u6784\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8def\u5f84\u68c0\u67e5\u51fd\u6570\u6765\u67e5\u627e\u6587\u4ef6\u540d\uff0c\u4e0d\u7136URL\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def check_url(path):\n    if path.startswith('http://'):\n        return Finder()\n    else:\n        raise ImportError()\nsys.path.append('http://localhost:15000')\nsys.path_hooks[0] = check_url\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Notice installation of Finder in sys.path_importer_cache\nsys.path_importer_cache['http://localhost:15000']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u5c31\u662f\u672c\u8282\u6700\u540e\u90e8\u5206\u7684\u5173\u952e\u70b9\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4e00\u4e2a\u7528\u6765\u5728sys.path\u4e2d\u67e5\u627eURL\u7684\u81ea\u5b9a\u4e49\u8def\u5f84\u68c0\u67e5\u51fd\u6570\u5df2\u7ecf\u6784\u5efa\u5b8c\u6bd5\u3002\n\u5f53\u5b83\u4eec\u88ab\u78b0\u5230\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u65b0\u7684 UrlPathFinder \u5b9e\u4f8b\u88ab\u521b\u5efa\u5e76\u88ab\u653e\u5165 sys.path_importer_cache.\n\u4e4b\u540e\uff0c\u6240\u6709\u9700\u8981\u68c0\u67e5 sys.path \u7684\u5bfc\u5165\u8bed\u53e5\u90fd\u4f1a\u4f7f\u7528\u4f60\u7684\u81ea\u5b9a\u4e49\u67e5\u627e\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u57fa\u4e8e\u8def\u5f84\u5bfc\u5165\u7684\u5305\u5904\u7406\u7a0d\u5fae\u6709\u70b9\u590d\u6742\uff0c\u5e76\u4e14\u8ddf find_loader() \u65b9\u6cd5\u8fd4\u56de\u503c\u6709\u5173\u3002\n\u5bf9\u4e8e\u7b80\u5355\u6a21\u5757\uff0cfind_loader() \u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(loader, None)\uff0c\n\u5176\u4e2d\u7684loader\u662f\u4e00\u4e2a\u7528\u4e8e\u5bfc\u5165\u6a21\u5757\u7684\u52a0\u8f7d\u5668\u5b9e\u4f8b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u4e00\u4e2a\u666e\u901a\u7684\u5305\uff0cfind_loader() \u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(loader, path)\uff0c\n\u5176\u4e2d\u7684loader\u662f\u4e00\u4e2a\u7528\u4e8e\u5bfc\u5165\u5305\uff08\u5e76\u6267\u884c__init__.py\uff09\u7684\u52a0\u8f7d\u5668\u5b9e\u4f8b\uff0c\npath\u662f\u4e00\u4e2a\u4f1a\u521d\u59cb\u5316\u5305\u7684 __path__ \u5c5e\u6027\u7684\u76ee\u5f55\u5217\u8868\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u57fa\u7840URL\u662f http://localhost:15000 \u5e76\u4e14\u4e00\u4e2a\u7528\u6237\u6267\u884c import grok ,\n\u90a3\u4e48 find_loader() \u8fd4\u56de\u7684path\u5c31\u4f1a\u662f [ \u2018http://localhost:15000/grok\u2019 ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "find_loader() \u8fd8\u8981\u80fd\u5904\u7406\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u5305\u3002\n\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u5305\u4e2d\u6709\u4e00\u4e2a\u5408\u6cd5\u7684\u5305\u76ee\u5f55\u540d\uff0c\u4f46\u662f\u4e0d\u5b58\u5728__init__.py\u6587\u4ef6\u3002\n\u8fd9\u6837\u7684\u8bdd\uff0cfind_loader() \u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(None, path)\uff0c\npath\u662f\u4e00\u4e2a\u76ee\u5f55\u5217\u8868\uff0c\u7531\u5b83\u6765\u6784\u5efa\u5305\u7684\u5b9a\u4e49\u6709__init__.py\u6587\u4ef6\u7684__path__\u5c5e\u6027\u3002\n\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u5bfc\u5165\u673a\u5236\u4f1a\u7ee7\u7eed\u524d\u884c\u53bb\u68c0\u67e5sys.path\u4e2d\u7684\u76ee\u5f55\u3002\n\u5982\u679c\u627e\u5230\u4e86\u547d\u540d\u7a7a\u95f4\u5305\uff0c\u6240\u6709\u7684\u7ed3\u679c\u8def\u5f84\u88ab\u52a0\u5230\u4e00\u8d77\u6765\u6784\u5efa\u6700\u7ec8\u7684\u547d\u540d\u7a7a\u95f4\u5305\u3002\n\u5173\u4e8e\u547d\u540d\u7a7a\u95f4\u5305\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u800310.5\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6240\u6709\u7684\u5305\u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u5185\u90e8\u8def\u5f84\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5728__path__\u5c5e\u6027\u4e2d\u770b\u5230\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import xml.etree.ElementTree\nxml.__path__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "xml.etree.__path__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e4b\u524d\u63d0\u5230\uff0c__path__\u7684\u8bbe\u7f6e\u662f\u901a\u8fc7 find_loader() \u65b9\u6cd5\u8fd4\u56de\u503c\u63a7\u5236\u7684\u3002\n\u4e0d\u8fc7\uff0c__path__\u63a5\u4e0b\u6765\u4e5f\u88absys.path_hooks\u4e2d\u7684\u51fd\u6570\u5904\u7406\u3002\n\u56e0\u6b64\uff0c\u4f46\u5305\u7684\u5b50\u7ec4\u4ef6\u88ab\u52a0\u8f7d\u540e\uff0c\u4f4d\u4e8e__path__\u4e2d\u7684\u5b9e\u4f53\u4f1a\u88ab handle_url() \u51fd\u6570\u68c0\u67e5\u3002\n\u8fd9\u4f1a\u5bfc\u81f4\u65b0\u7684 UrlPathFinder \u5b9e\u4f8b\u88ab\u521b\u5efa\u5e76\u4e14\u88ab\u52a0\u5165\u5230 sys.path_importer_cache \u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e2a\u96be\u70b9\u5c31\u662f handle_url() \u51fd\u6570\u4ee5\u53ca\u5b83\u8ddf\u5185\u90e8\u4f7f\u7528\u7684 _get_links() \u51fd\u6570\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\n\u5982\u679c\u4f60\u7684\u67e5\u627e\u5668\u5b9e\u73b0\u9700\u8981\u4f7f\u7528\u5230\u5176\u4ed6\u6a21\u5757\uff08\u6bd4\u5982urllib.request\uff09\uff0c\n\u6709\u53ef\u80fd\u8fd9\u4e9b\u6a21\u5757\u4f1a\u5728\u67e5\u627e\u5668\u64cd\u4f5c\u671f\u95f4\u8fdb\u884c\u66f4\u591a\u7684\u5bfc\u5165\u3002\n\u5b83\u53ef\u4ee5\u5bfc\u81f4 handle_url() \u548c\u5176\u4ed6\u67e5\u627e\u5668\u90e8\u5206\u9677\u5165\u4e00\u79cd\u9012\u5f52\u5faa\u73af\u72b6\u6001\u3002\n\u4e3a\u4e86\u89e3\u91ca\u8fd9\u79cd\u53ef\u80fd\u6027\uff0c\u5b9e\u73b0\u4e2d\u6709\u4e00\u4e2a\u88ab\u521b\u5efa\u7684\u67e5\u627e\u5668\u7f13\u5b58\uff08\u6bcf\u4e00\u4e2aURL\u4e00\u4e2a\uff09\u3002\n\u5b83\u53ef\u4ee5\u907f\u514d\u521b\u5efa\u91cd\u590d\u67e5\u627e\u5668\u7684\u95ee\u9898\u3002\n\u53e6\u5916\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u53ef\u4ee5\u786e\u4fdd\u67e5\u627e\u5668\u4e0d\u4f1a\u5728\u521d\u59cb\u5316\u94fe\u63a5\u96c6\u5408\u7684\u65f6\u5019\u54cd\u5e94\u4efb\u4f55\u5bfc\u5165\u8bf7\u6c42\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Check link cache\nif self._links is None:\n    self._links = [] # See discussion\n    self._links = _get_links(self._baseurl)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u67e5\u627e\u5668\u7684 invalidate_caches() \u65b9\u6cd5\u662f\u4e00\u4e2a\u5de5\u5177\u65b9\u6cd5\uff0c\u7528\u6765\u6e05\u7406\u5185\u90e8\u7f13\u5b58\u3002\n\u8fd9\u4e2a\u65b9\u6cd5\u518d\u7528\u6237\u8c03\u7528 importlib.invalidate_caches() \u7684\u65f6\u5019\u88ab\u89e6\u53d1\u3002\n\u5982\u679c\u4f60\u60f3\u8ba9URL\u5bfc\u5165\u8005\u91cd\u65b0\u8bfb\u53d6\u94fe\u63a5\u5217\u8868\u7684\u8bdd\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u6bd4\u4e0b\u4e24\u79cd\u65b9\u6848\uff08\u4fee\u6539sys.meta_path\u6216\u4f7f\u7528\u4e00\u4e2a\u8def\u5f84\u94a9\u5b50\uff09\u3002\n\u4f7f\u7528sys.meta_path\u7684\u5bfc\u5165\u8005\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u7684\u9700\u8981\u81ea\u7531\u5904\u7406\u6a21\u5757\u3002\n\u4f8b\u5982\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u5bfc\u5165\u6216\u4ee5\u4e0d\u540c\u4e8e\u4e00\u822c\u6a21\u5757/\u5305\u5904\u7406\u65b9\u5f0f\u5bfc\u5165\u3002\n\u8fd9\u79cd\u81ea\u7531\u540c\u6837\u610f\u5473\u7740\u5bfc\u5165\u8005\u9700\u8981\u81ea\u5df1\u8fdb\u884c\u5185\u90e8\u7684\u4e00\u4e9b\u7ba1\u7406\u3002\n\u53e6\u5916\uff0c\u57fa\u4e8e\u8def\u5f84\u7684\u94a9\u5b50\u53ea\u662f\u9002\u7528\u4e8e\u5bf9sys.path\u7684\u5904\u7406\u3002\n\u901a\u8fc7\u8fd9\u79cd\u6269\u5c55\u52a0\u8f7d\u7684\u6a21\u5757\u8ddf\u666e\u901a\u65b9\u5f0f\u52a0\u8f7d\u7684\u7279\u6027\u662f\u4e00\u6837\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u5230\u73b0\u5728\u4e3a\u6b62\u4f60\u8fd8\u662f\u4e0d\u662f\u5f88\u660e\u767d\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u4e00\u4e9b\u65e5\u5fd7\u6253\u5370\u6765\u6d4b\u8bd5\u4e0b\u672c\u8282\u3002\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import logging\nlogging.basicConfig(level=logging.DEBUG)\nimport urlimport\nurlimport.install_path_hook()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path.append('http://localhost:15000')\nimport fib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5efa\u8bae\u4f60\u82b1\u70b9\u65f6\u95f4\u770b\u770b PEP 302\n\u4ee5\u53caimportlib\u7684\u6587\u6863\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}